package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.impl.impl.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ApplicationListener:spring的监听器
 * ContextRefreshedEvent:表示spring的初始化事件(完成IoC操作)
 */
@Component
public class HotDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Value("${hotData.init}")
    private Boolean flag;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        //注意:可以通过查看Redis看数据添加成功了没
        //System.out.println(strategyDetailService.getClass());   class com.sun.proxy.$Proxy68
        //System.out.println(strategyDetailService.getClass());
        //System.out.println(strategyStatsCacheService.getClass());
        //完成数据迁移初始化
        if (flag) {
            //1.把数据从mysql中查询出来
            List<StrategyDetail> detailList = strategyDetailService.list();
            //循环遍历目的是为了封装数据
            for (StrategyDetail detail : detailList) {

                //先判断统计对象是否存在?
                //strategyStatsCacheService

                //System.out.println(detail);
                Destination dest = detail.getDest();
                //2.封装统计数据
                StrategyStats ss = new StrategyStats();
                ss.setDestId(dest.getId());
                ss.setDestName(dest.getName());
                ss.setTitle(detail.getTitle());
                //攻略id
                ss.setStrategyId(detail.getId());
                //浏览数
                ss.setViewnum(detail.getViewnum());
                //收藏数
                ss.setFavornum(detail.getFavornum());
                //评论数
                ss.setReplynum(detail.getReplynum());
                //分享数
                ss.setSharenum(detail.getSharenum());
                //点赞数
                ss.setThumbsupnum(detail.getThumbsupnum());
                //3.文章的缓存服务保存到Redis中
                strategyStatsCacheService.setStrategyStats(ss);


                Long id = detail.getId();
                /*//排行榜初始化数据  文章的阅读数+评论数作为分值
                if (strategyStatsCacheService.notExistUpRank(id)) {
                    //阅读数+评论数作为分值
                    int num = detail.getViewnum() + detail.getReplynum();
                    strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_UP_SORT,num,id);
                }
                //文章的点赞数+收藏数作为分值
                if (strategyStatsCacheService.notExistHotRank(id)){
                    int num = detail.getFavornum() + detail.getThumbsupnum();
                    strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_HOT_SORT, num, id);
                }*/
               //排行榜初始化数据  文章的阅读数+评论数作为分值
                if (strategyStatsCacheService.notExistRank(RedisKey.STRATEGY_STATS_UP_SORT,id)) {
                    //阅读数+评论数作为分值
                    int num = detail.getViewnum() + detail.getReplynum();
                    strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_UP_SORT,num,id);
                }
                //文章的点赞数+收藏数作为分值
                if (strategyStatsCacheService.notExistRank(RedisKey.STRATEGY_STATS_HOT_SORT,id)){
                    int num = detail.getFavornum() + detail.getThumbsupnum();
                    strategyStatsCacheService.addRank(RedisKey.STRATEGY_STATS_HOT_SORT, num, id);
                }
            }
        }

    }


}
